require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'collection',
    'model',
    'dialog',
    'datepicker'
], function ($, common, ready, template, Collection, Model, dialog, datepicker) {

    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {

            this.event();
            this.fetch();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                container: 'data-list',
                template: 'data-item',
                url: _global.url.api + 'user/master/user_list',
                condition: {
                    limit: 10,
                    offset: 0
                },
                model: {
                    tag_name: 'tr',
                    url: _global.url.api + '',
                    event: {
                        '#btn-add_banned_user:click': 'add_banned_user',
                        '#btn-del_banned_user:click': 'del_banned_user',
                        '#btn-add_user_tag': 'add_user_tag'
                    },
                    add_banned_user: function (e) {
                        e.preventDefault();
                        var _this = this;
                        dialog({
                            content: '确定要加入黑名单吗?',
                            ok: function () {
                                $.ajax({
                                    url: _global.url.api + 'user/master/banned_user',
                                    type: 'put',
                                    data: {
                                        'user_id': _this.data.id,
                                        'act': 'add'
                                    },
                                    success: function (res) {
                                        if (res.code == 0) {
                                            _this.data.is_banned_user = 1;
                                            _this.create_element();
                                        }
                                    },
                                    error: function () {
                                        Alert({msg: '通讯错误', type: 'error'});
                                    }
                                });
                            },
                            cancel: function () {
                            }
                        }).show(e.target);
                    },
                    del_banned_user: function (e) {
                        e.preventDefault();
                        var _this = this;
                        dialog({
                            content: '确定要移除黑名单吗?',
                            ok: function () {
                                $.ajax({
                                        url: _global.url.api + 'user/master/banned_user',
                                        type: 'put',
                                        data: {
                                            'user_id': _this.data.id,
                                            'act': 'del'
                                        },
                                        success: function (res) {
                                        if (res.code == 0) {
                                            _this.data.is_banned_user = 0;
                                            _this.create_element();
                                        }
                                    },
                                    error: function () {
                                        Alert({msg: '通讯错误', type: 'error'});
                                    }
                                });
                            },
                            cancel: function () {
                            }
                        }).show(e.target);
                    },
                    add_user_tag: function (e) {
                        var Tthis = this;
                        var dialogObj =
                            dialog({
                            title: '用户打标',
                            width: 200,
                            modal: true,
                            content: template('add-user-tag', {dataTag: Tthis.data.tag_data}),
                            ok: function() {
                                var price_config = new Array();
                                $("input[name='user_tag']:checked").each(function (i) {
                                    var sku_id = $.trim($(this).val());
                                    price_config.push(sku_id);
                                });

                                var dataTag = {
                                    'user_id': Tthis.data.id,
                                    'user_tag': price_config
                                };
                                new Model({
                                    url: _global.url.api + 'user/master/user_add_tag',
                                    data: dataTag,
                                    success: function() {
                                        _this.collection.reload();//loaded();//移除加载效果
                                    }
                                }).post();
                            }, cancel: function() {}
                        }).show();
                    }
                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function () {
            var _this = this;

            //日期
            $('.long_date').on('click', function () {
                WdatePicker({
                    el: this.id,
                    dateFmt: 'yyyy-MM-dd HH:mm'
                });
            });

            $('#btn-search').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                _this.collection.condition.sexstatus = $('#sexstatus').val();
                _this.collection.condition.begintime = $('input[name="begintime"]').val();
                _this.collection.condition.endtime = $('input[name="endtime"]').val();
                _this.collection.reload();
            });

            /**
             * 按状态查询
             */
            $('.btn-banned_user').on('click', function (e) {
                e.preventDefault();

                _this.collection.condition.status = $(e.target).attr('rel');
                _this.collection.reload();
                $(e.target).addClass('active').siblings().removeClass('active');
            });
        }
    };

    new index();

});